
project , uses(../data_output/legislators_klarner.dta)


	***********
	** Montana
	** Consecutive term limites: 4 x 2-year terms.
	** First year: 2000	
	***********	
	use ../data_output/legislators_klarner.dta, clear
	keep if state=="MT" & chamber=="house"
	keep if regexm(term, "2001|2003|2005|2007|2009|2011|2013|2015")
	gen lastname = trim(regexs(1)) if regexm(lower(CandName),"([^,]+)")
	gen firstname = trim(regexs(1)) if regexm(lower(CandName),",( [^ ]+)")
	gen mergename = firstname +" "+lastname
	bys chamber mergename term: gen n = _n
	by chamber mergename term: egen mean_n=mean(n)
	replace mergename = CandName if mean_n>1  //firstname  + " " +  lastname //firstname+ ". "
	drop n mean_n
	save ../data_output/legislators_MT_house_2001_2016.dta, replace
	
	gen year_minus0 = year	
	gen year_minus2 = year-2
	gen year_minus4 = year-4
	gen year_minus6 = year-6
	tostring year_minus*, replace
	gen s = year_minus6+";" +year_minus4+";"+year_minus2+";"+ year_minus0 + ";"
	gen termlimited = regexm(elections_won, s)==1 & year>=1998
	save ../data_output/legislators_MT_house_2001_2016.dta, replace
project, creates(../data_output/legislators_MT_house_2001_2016.dta)

project, original(../data_input/MT/mt_fiscal.csv)
	import delimited using ../data_input/MT/mt_fiscal.csv,  clear  delimiters(";") bindquotes(strict) encoding("utf-8") varnames(1)
	drop if regexm(bill, "_[2-9]")
	replace bill = upper(bill)
	replace bill = regexs(1) if regexm(bill, "(.*)[_.]")
	replace fiscal_impact = subinstr(fiscal_impact, ")" , "" , .)
	replace fiscal_impact = subinstr(fiscal_impact, "($ " , "-" , .)
	replace fiscal_impact = subinstr(fiscal_impact, "($" , "-" , .)
	replace fiscal_impact = subinstr(fiscal_impact, "(" , "-" , .)
	replace fiscal_impact = subinstr(fiscal_impact, "$" , "" , .)
	replace fiscal_impact = subinstr(fiscal_impact, "  " , " " , .)
	split fiscal_impact, gen(fiscal_impact_) 
	destring fiscal_impact_*, replace force
	egen fiscal_effect = rowmean(fiscal_impact_*)
	tempfile fiscal
	save `fiscal'
	

project, original(../data_input/MT/mt_bills.csv)	
	import delimited using ../data_input/MT/mt_bills.csv,  clear  delimiters(";") bindquotes(strict) encoding("utf-8") varnames(1)
	keep if chamber=="H"
	replace chamber = "house"
	merge 1:1 session bill using `fiscal'
	drop if _merge==2
	drop _merge
	replace fiscal_effect = 0 if fiscal_effect==.
	gen fiscal_notes = fiscal_note!=""
	tostring session, replace
	gen term = ""	
	replace term = "2001-2002" if regexm(session, "2001|2002")
	replace term = "2003-2004" if regexm(session, "2003|2004")
	replace term = "2005-2006" if regexm(session, "2005|2006")
	replace term = "2007-2008" if regexm(session, "2007|2008")
	replace term = "2009-2010" if regexm(session, "2009|2010")
	replace term = "2011-2012" if regexm(session, "2011|2012")
	replace term = "2013-2014" if regexm(session, "2013|2014")
	replace term = "2015-2016" if regexm(session, "2015|2016")
	replace sponsor = subinstr(sponsor,"  "," ",.)
	gen firstname = lower(regexs(1)) if regexm(sponsor,"(^[^ ]+)")
	gen lastname =  lower(regexs(1)) if regexm(sponsor,"([^ ]+) [(][RD][)]")
	gen mergename = firstname + " " + lastname
	drop firstname lastname	
	replace mergename = subinstr(mergename, "'","",.)
	replace mergename = subinstr(mergename, "-","",.)
	replace mergename = "daniel mcgee" if mergename=="dan mcgee" & regexm(term, "1999|2001")	
	replace mergename = "jack ross" if mergename=="john ross" & regexm(term, "2003|2005|2007")
	replace mergename = "don jones" if mergename=="donald jones" & regexm(term, "2013|2015")
	replace mergename = "jennifer pomnichowski" if mergename=="jp pomnichowski" & regexm(term, "2007|2009|2013")
	replace mergename = "deb kottel" if mergename=="deborah kottel" & regexm(term, "2007|2009")
	replace mergename = "jenny eck" if mergename=="jennifer eck" & regexm(term, "2013|2015")
	replace mergename = "jesse ohara" if mergename=="james ohara" & regexm(term, "2007|2009|2011|2013")
	replace mergename = "patricia peppers" if mergename=="rae peppers" & regexm(term, "2013|2015")
	replace mergename = "patrick connell" if mergename=="pat connell" & regexm(term, "2011|2013")
	replace mergename = "joseph tropila" if mergename=="joe tropila" & regexm(term, "1999|2001")
	replace mergename = "william glaser" if mergename=="bill glaser" & regexm(term, "2005|2007|2009")
	replace mergename = "robert story" if mergename=="bob story" & regexm(term, "1999|2001")
	replace mergename = "william thomas" if mergename=="bill thomas" & regexm(term, "1999|2001|2003|2007")
	replace mergename = "william wilson" if mergename=="bill wilson" & regexm(term, "2003|2005|2007|2009")
	replace mergename = "t. callahan" if mergename=="tim callahan" & regexm(term, "2001|2003|2005|2007")
	replace mergename = "kendall vandyk" if mergename=="kendall dyk" & regexm(term, "2007|2009")
	replace mergename = "tim furey" if mergename=="timothy furey" & regexm(term, "2009|2011")
	replace mergename = "jonathan windyboy" if mergename=="jonathan boy" & regexm(term, "2003|2005|2007")
	replace mergename = "forrestina calfbossribs" if mergename=="forrestina ribs" & regexm(term, "2013")
	replace mergename = "gilbert meyers" if mergename=="g. meyers" & regexm(term, "2015")
	replace mergename = "katharin kelker" if mergename=="kathy kelker" & regexm(term, "2015")
	replace mergename = "randy pinocci" if mergename=="randall pinocci" & regexm(term, "2015")
	replace mergename = "robert brown" if mergename=="bob brown" & regexm(term, "2015")

	merge m:1  mergename term using ../data_output/legislators_MT_house_2001_2016.dta	
	*15 obs do not merge
	
	drop if _merge==1
	drop sponsor
	gen sponsor = 1 if bill!=""
	gen law = 0
	replace law = 1 if regexm(lower(status), "chapter number assign|signed by governor")
	gen sponsor_approp = regexm( lower(cmt_assign) , "approp")
	gen billlength = length(billtext)
	save ../data_output/bills_by_sponsor_MT_2001_2016.dta, replace
project , creates(../data_output/bills_by_sponsor_MT_2001_2016.dta)	
	
	use ../data_output/bills_by_sponsor_mt_2001_2016.dta, clear
	gen fiscal_analysis = fiscal_note!=""
	gen fiscal_positive = fiscal_effect>0
	gen fiscal_negative = fiscal_effect<0
	gen fiscal_neutral = fiscal_effect==0	
	gen law_fiscal_effect = 0 if fiscal_effect!=.
	replace law_fiscal_effect = fiscal_effect if law==1
	
	collapse (sum) fiscal_analysis fiscal_positive fiscal_negative fiscal_neutral law sponsor* fiscal_notes billlength   total_fiscal_effect = fiscal_effect (mean) law_fiscal_effect mean_fiscal_effect = fiscal_effect  (first) elections_* terms_in_office win TermLengthActual CandName district chamber state party mergename inoffice year termlimited vtsh, by(CandId term )

	foreach v of varlist sponsor_approp billlength  law *fiscal* {
	bys  term: egen max = max(`v')
		replace `v' = . if max==0
		drop max
	}	
	
	save ../data_output/total_bills_by_sponsor_MT_2001_2016.dta, replace	
project , creates(../data_output/total_bills_by_sponsor_MT_2001_2016.dta)	
	
	

	
	


	
	**Voting
	
	use ../data_output/legislators_klarner.dta, clear
	gen lastname = regexs(1) if regexm(lower(CandName),"([^,]+), [^!]")
	gen firstname = regexs(1) if regexm(lower(CandName),"[^,]+, ([^ ]+)")
	gen firstletter = regexs(1) if regexm(lower(CandName),"[^,]+, ([^ ])")
	keep if state=="MT" & chamber=="house"
	keep if regexm(term, "2001|2003|2005|2007|2009|2011|2013|2015")
	gen os_mergename = ""
	replace os_mergename = lastname if regexm(term, "2001|2003|2005|2007")
	bys chamber os_mergename term: gen n = _n
	by chamber os_mergename term: egen mean_n=mean(n)
	replace os_mergename = firstletter+". " +lastname if mean_n>1  //firstname  + " " +  lastname //firstname+ ". "
	drop n mean_n		
	replace os_mergename = lastname+", " + firstname if regexm(term, "2009|2011|2013|2015")
	tempfile tmp
	save `tmp', replace
	

project, original(../data_input/MT/mt_votes.csv)
	import delimited using ../data_input/MT/mt_votes.csv,  clear  delimiters(";") bindquotes(strict) encoding("utf-8") varnames(1)
	tostring session, replace
	keep if chamber=="house"
	gen term = ""
	replace term = "2001-2002" if regexm(session, "2001|2002")
	replace term = "2003-2004" if regexm(session, "2003|2004")
	replace term = "2005-2006" if regexm(session, "2005|2006")
	replace term = "2007-2008" if regexm(session, "2007|2008")
	replace term = "2009-2010" if regexm(session, "2009|2010")
	replace term = "2011-2012" if regexm(session, "2011|2012")
	replace term = "2013-2014" if regexm(session, "2013|2014")
	replace term = "2015-2016" if regexm(session, "2015|2016")
	gen os_mergename = trim(lower(name))
	replace os_mergename = lower(os_mergename)
	gen lastname = regexs(1) if regexm(lower(name),"([^,]+), [^!]")
	gen firstname = regexs(1) if regexm(lower(name),"[^,]+, ([^ ]+)")	
	replace os_mergename = lastname+", " + firstname if regexm(term, "2009|2011|2013|2015") 
	drop firstname lastname
	replace os_mergename = subinstr(os_mergename,"’","",.)
	replace os_mergename = subinstr(os_mergename,"'","",.)
	replace os_mergename = subinstr(os_mergename,"-","",.)	
	replace os_mergename = subinstr(os_mergename,".",". ",.)
	replace os_mergename = subinstr(os_mergename,"  "," ",.)
	replace os_mergename = "mcgee"	if os_mergename=="mr. speaker" & regexm(term, "2001")
	replace os_mergename = "mood"	if os_mergename=="mr. speaker" & regexm(term, "2003")
	replace os_mergename = "matthews"	if os_mergename=="mr. speaker" & regexm(term, "2005")
	replace os_mergename = "sales"	if os_mergename=="mr. speaker" & regexm(term, "2007")
	replace os_mergename = "windyboy"	if os_mergename=="windy boy" & regexm(term, "2003|2005|2007")
	replace os_mergename = "vandyk"	if os_mergename=="van dyk" & regexm(term, "2007")
	replace os_mergename = "furey, tim"	if os_mergename=="furey, timothy" & regexm(term, "2009|2011")
	replace os_mergename = "glaser, william"	if os_mergename=="glaser, bill" & regexm(term, "2009")
	replace os_mergename = "kottel, deb"	if os_mergename=="kottel, deborah" & regexm(term, "2009")
	replace os_mergename = "pomnichowski, jennifer"	if os_mergename=="pomnichowski, jp" & regexm(term, "2009|2013")
	replace os_mergename = "vandyk, kendall"	if os_mergename=="van dyk, kendall" & regexm(term, "2009")
	replace os_mergename = "wilson, william"	if os_mergename=="wilson, bill" & regexm(term, "2009")	
	replace os_mergename = "connell, patrick"	if os_mergename=="connell, pat" & regexm(term, "2011|2013")
	replace os_mergename = "ohara, jesse"	if os_mergename=="ohara, james" & regexm(term, "2009|2011|2013")
	replace os_mergename = "calfbossribs, forrestina"	if os_mergename=="calf boss ribs, forrestina" & regexm(term, "2013")
	replace os_mergename = "eck, jenny"	if os_mergename=="eck, jennifer" & regexm(term, "2013|2015")
	replace os_mergename = "jones, don"	if os_mergename=="jones, donald" & regexm(term, "2013|2015")
	replace os_mergename = "peppers, patricia"	if os_mergename=="peppers, rae" & regexm(term, "2013|2015")
	replace os_mergename = "brown, robert"	if os_mergename=="brown, bob" & regexm(term, "2013|2015")
	replace os_mergename = "kelker, katharin"	if os_mergename=="kelker, kathy" & regexm(term, "2013|2015")
	replace os_mergename = "meyers, gilbert"	if os_mergename=="meyers, g. " & regexm(term, "2013|2015")
	replace os_mergename = "pinocci, randy"	if os_mergename=="pinocci, randall" & regexm(term, "2015")
	replace os_mergename = "roundstone, j."	if os_mergename=="roundstone, j. " & regexm(term, "2009")
	replace os_mergename = "ribs, frosty"	if os_mergename=="boss ribs, frosty" & regexm(term, "2009")

	merge m:1  os_mergename term using `tmp'	
	// 3 obs not merging
	drop if _merge==1
	drop _merge	
	
	gen floorvotes = regexm(lower(vote), "y|n")
	replace vote = "1" if regexm(lower(vote),"y")
	replace vote = "-1" if regexm(lower(vote),"n")
	replace vote = "0" if vote!="1" & vote!="-1"
	destring vote, replace
	egen vote_id =  group(session chamber bill motion date) , missing
	bys party vote_id : egen party_mean=mean(vote)
	gen party_vote = 0
	replace party_vote = 1 if party_mean>0
	replace party_vote = -1 if party_mean<0
	gen vote_party_line = vote==party_vote
	gen vote_against_party = vote==-party_vote
	bys vote_id: egen votemargin = mean(vote)
	gen close_floorvotes = floorvotes if abs(votemargin)<.1		
	
	save ../data_output/floorvotes_MT_2001_2016.dta, replace
project, creates(../data_output/floorvotes_MT_2001_2016.dta)	
	
	use ../data_output/floorvotes_MT_2001_2016.dta, clear
	collapse  (mean) vote_party_line vote_against_party (sum) *floorvotes (first) elections_* terms_in_office win TermLengthActual CandName district chamber state party  inoffice year  vtsh, by(CandId term )
	
	save ../data_output/total_floorvotes_MT_2001_2016.dta, replace
project, creates(../data_output/total_floorvotes_MT_2001_2016.dta)	
	
	
